﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using Common.Logging;
using Quartz;
using System.Threading;
using Quartz.Impl;
using System.Collections;

namespace stock
{
    public class Global : System.Web.HttpApplication
    {
        private static ILog log = LogManager.GetLogger(typeof(Global));
        IScheduler scheduler;
        protected void Application_Start(object sender, EventArgs e)
        {
            log.Info("------- Application Start ---------------------");
            ISchedulerFactory Isf = new StdSchedulerFactory();
            scheduler = Isf.GetScheduler();
            CronTriggerRunner(scheduler);
            Hashtable stockHash = new Hashtable();
            List<Common.Model.StockVirtual> Virtuals = (new BLL.Tools()).GetAll();
            if (Virtuals != null && Virtuals.Count > 0)
            {
                foreach (Common.Model.StockVirtual virtualModel in Virtuals)
                {
                    stockHash[virtualModel.StockCode] = virtualModel;
                }
                BLL.globalObject.GlobalHash = stockHash;
            }            
        }
        private static void CronTriggerRunner(IScheduler scheduler)
        {
            log.Info("------- Scheduling Jobs ----------------");
            CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1",  "0 30/1 9-11 ? * 2-6");
            JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob));
            job.JobDataMap.Add("job", 1);
            scheduler.ScheduleJob(job, trigger);
            JobDetail job13 = new JobDetail("job13", "group13", typeof(SimpleQuartzJob));
            job13.JobDataMap.Add("job", 1);
            CronTrigger trigger13 = new CronTrigger("trigger13", "group13", "job13", "group13");
            trigger13.CronExpressionString = "0/50 * 13-14 ? * 2-6";
            scheduler.ScheduleJob(job13, trigger13);
            #region///--天气--
            CronTrigger triggerWeather = new CronTrigger("triggerWeather", "groupWeather", "jobWeather", "groupWeather", "0 1 8 ? * 1-7");
            JobDetail jobWeather = new JobDetail("jobWeather", "groupWeather", typeof(MonitorJob));
            jobWeather.JobDataMap.Add("job", 1);
            scheduler.ScheduleJob(jobWeather, triggerWeather);
            #endregion
            scheduler.Start();
            log.Info("---schedure start-----------");
        }
        private static void SimpleTriggerRunner(IScheduler scheduler)
        {
            JobDetail job = new JobDetail("job_1", "job1", typeof(SimpleQuartzJob));
            job.JobDataMap.Add("job", 1);
            SimpleTrigger triger = new SimpleTrigger("trig1", "trig1");
            triger.StartTimeUtc = TriggerUtils.GetNextGivenSecondDate(null, 4);
            triger.RepeatInterval = new TimeSpan(10000);
            triger.RepeatCount = 8;
            scheduler.ScheduleJob(job, triger);
            scheduler.Start();
            log.Info("---schedure start-----------");            
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string OldDomain = Request.ServerVariables["Http_Host"].ToLower();
            string oldUrl = HttpContext.Current.Request.RawUrl;
            String temp = OldDomain + "/test";
            if (OldDomain == "www.telwo.com")
            {
                int i = oldUrl.IndexOf(temp);
                if (i == -1)
                {
                    this.Context.RewritePath("/test" + Request.Path + "?" + Request.QueryString);
                }
            }                        
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            //在应用程序关闭时运行的代码
            System.Threading.Thread.Sleep(2000);
            String str = "http://wap.telwo.com";
            Uri uri = new Uri(str);
            System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.Stream str3 = resp.GetResponseStream();
            System.IO.StreamReader st = new System.IO.StreamReader(str3, System.Text.Encoding.Default);
            String s = st.ReadToEnd().Trim();
            System.Threading.Thread.Sleep(2000);
            log.Info("------- Shutting Down ---------------------");
            scheduler.Shutdown();
        }
    }
}